package com.daafan.minilib.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.daafan.minilib.R;
import com.daafan.minilib.data.Library;
import com.daafan.minilib.data.TempBookList;

public class TempBooksActivity extends BaseActivity {

	ListView			listView_temp_books;
	TempBooksAdpater	mAdapter;
	List<Library>		bookList	= new ArrayList<Library>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_temp_books);

		initTopBar("临时书架", R.drawable.back, 0);

		listView_temp_books = (ListView) findViewById(R.id.listView_temp_books);

		mAdapter = new TempBooksAdpater();
		listView_temp_books.setAdapter(mAdapter);

		restoreTempBookList();
	}

	private void restoreTempBookList() {
		TempBookList tempBookList = new TempBookList();
		tempBookList.restore(mContext);
		bookList = tempBookList.datas;

		if (bookList.size() == 0)
			Toast.makeText(mContext, "请使用二维码扫描或摇一摇，收藏书刊", Toast.LENGTH_LONG).show();
	}

	@Override
	protected void onTopLeftClick() {
		back();
	}

	class TempBooksAdpater extends BaseAdapter {

		ViewHolder	holder;

		class ViewHolder {
			TextView	tv_book;
			TextView	tv_book_category;
			Button		btn_delete;
		}

		@Override
		public int getCount() {
			return bookList.size();
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.item_temp_books, null);

				holder = new ViewHolder();
				holder.tv_book = (TextView) convertView.findViewById(R.id.tv_temp_book);
				holder.tv_book_category = (TextView) convertView.findViewById(R.id.tv_temp_book_category);
				holder.btn_delete = (Button) convertView.findViewById(R.id.btn_temp_book_delete);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			if (position % 2 == 0) {
				convertView.setBackgroundColor(getResources().getColor(R.color.temp_book_bg));
			} else {
				convertView.setBackgroundColor(Color.TRANSPARENT);
			}

			// 该项图书
			final Library library = bookList.get(position);

			holder.btn_delete.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					showConfirmDialog("提示", "是否删除?", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							db.deleteByWhere(Library.class, "lid=" + library.lid);

							handler.post(new Runnable() {

								@Override
								public void run() {
									restoreTempBookList();
									mAdapter.notifyDataSetChanged();
								}
							});
						}
					});
				}
			});

			holder.tv_book.setText(library.name);
			holder.tv_book_category.setVisibility(View.GONE);

			convertView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Library library = bookList.get(position);

					Intent intent = new Intent(mContext, WebViewActivity.class);
					intent.putExtra(WebViewActivity.URL, library.url);
					intent.putExtra(WebViewActivity.TITLE, library.name);
					startActivity(intent);
				}
			});

			return convertView;
		}
	}
}
